package com.gxa.zhonglong.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.RoleMapper;
import com.gxa.zhonglong.pojo.Role;
import com.gxa.zhonglong.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @ClassName RoleServiceImpl
 * @Author: ws
 * @date 2021/10/27 9:16
 */
@Service("RoleService")
@Transactional(rollbackFor = {Throwable.class})
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Integer add(Role role) throws BusinessException {
        Map<String, Object> map = roleMapper.selectByName(role.getRoleName());
        if (map != null) {
            // 抛出异常，角色已存在
            throw new BusinessException(ExceptionCode.DUPLICATE_ROlE_NAME);
        } else {
            Integer add = roleMapper.add(role);
            if (add == 1) {
                return add;
            }else {
                throw new BusinessException(ExceptionCode.ADD_DEFEATED);
            }
        }

    }

    @Override
    public Integer delete(Long id) throws BusinessException {
        Integer delete = roleMapper.delete(id);
        if (delete == 1) {
            return delete;
        }else {
            throw new BusinessException(ExceptionCode.DELETE_FALSE);
        }

    }

    @Override
    public Integer update(Role role) throws BusinessException {
        Integer update = roleMapper.update(role);
        if (update == 1) {
            return update;
        }else {
            throw new BusinessException(ExceptionCode.UPDATE_FALSE);
        }
    }

    @Override
    public List<Map<String, Object>> getAll() {
        return roleMapper.getAll();
    }
}
